﻿using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace Halk.Config
{
    public class ConfigManage
    {
        private static ConfigManage _configManage;

        public static ConfigManage Instace()
        {
            return _configManage ?? (_configManage = new ConfigManage());
        }

        private List<ConfigInfo> _configs;

        private Dictionary<string, RuleEntity> _dicReserveRules; 

        static readonly object ObjLock = new object();
        /// <summary>
        /// 初始化配置文件
        /// </summary>
        public void Load()
        {
            lock (ObjLock)
            {
                _configs = new List<ConfigInfo>();
                using (FileStream stream = File.Open(@"conf\config.xml", FileMode.Open))
                {
                    var xmlserializer = new XmlSerializer(typeof (List<ConfigInfo>));
                    _configs = xmlserializer.Deserialize(stream) as List<ConfigInfo>;
                    stream.Close();
                    stream.Dispose();
                }
                if (_configs != null &&_configs.Count >0)
                {
                    _index = 0;
                }
            }
        }

        public void Save()
        {
            using (FileStream stream = File.Open("conf/config.xml", FileMode.Open))
            {
                var xmlserializer = new XmlSerializer(typeof(List<ConfigInfo>));
                xmlserializer.Serialize(stream, _configs);
            }
        }

        private int _index = -1;

        /// <summary>
        /// 获取当前配置
        /// </summary>
        /// <returns></returns>
        public ConfigInfo GetConfig()
        {
            return _configs[_index];
        }

        /// <summary>
        /// 移动到下一个配置
        /// </summary>
        public void MoveNext()
        {
            if (_index < _configs.Count-1)
                _index++;
        }

        /// <summary>
        /// 
        /// </summary>
        public void MoveFirst()
        {
            if (_configs.Count > 0)
                _index = 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int GetSize()
        {
            if (_configs == null)
                return 0;
            return _configs.Count;
        }

        public RuleEntity GetReserveRule(string flag)
        {
            if (_dicReserveRules == null||_dicReserveRules.Count==0)
            {
                LoadReserveRules();
            }
            if (_dicReserveRules != null) return _dicReserveRules[flag];
            return null;
        }

        private void LoadReserveRules()
        {
            if (_dicReserveRules == null)
                _dicReserveRules = new Dictionary<string, RuleEntity>();

            var arrs = Instace().GetConfig().ReserveRules.Split(';');
            foreach (string arr in arrs)
            {
                if (string.IsNullOrEmpty(arr)) continue;
                var second = RuleManage.Instace().GetRuleFormXml(string.Format("conf/rule/{0}.xml", arr));
                if (_dicReserveRules.ContainsKey(arr)) continue;
                second.RuleID.Pattern = arr;//不需要手工维护RuleID
                _dicReserveRules[arr] = second;
            }
        }

        public Dictionary<string, RuleEntity> GetReserveRuleList()
        {
            if (_dicReserveRules == null || _dicReserveRules.Count == 0)
            {
                LoadReserveRules();
            }
            return _dicReserveRules;
        }
    }
}
